# -*- coding: utf-8 -*-
# @Time    : 2019/10/14 19:40
# @Author  : LiHaowen
# @Email   : lihaowen2017@163.com
# @File    : abc_test.py

# 我们去检查某个类是否有某种方法


class Company(object):
    def __init__(self, employee_list):
        self.employee = employee_list

    # def __getitem__(self, item):
    #     # 变为可迭代类型
    #     return self.employee[item]

    def __len__(self):
        return len(self.employee)


company = Company(["tom", "bob", "jane"])
print(hasattr(company, "__len__"))  # 判断对象是否有某属性
print(len(company))
# 我们在某些情况之下希望判定某个对象的类型
from collections.abc import Sized

print(isinstance(company, Sized))


# 我们需要强制某个子类必须实现某些方法
# 实现了一个web框架，集成cache(redis, cache, memorycache)
# 需要设计一个抽象基类， 指定子类必须实现某些方法

# 如何取模拟一个抽象基类
import abc
from collections.abc import *


class CacheBase(metaclass=abc.ABCMeta):
    @abc.abstractmethod
    def get(self, key):
        pass

    @abc.abstractmethod
    def set(self, key, value):
        pass
#     def get(self, key):
#         raise NotImplementedError
#
#     def set(self, key, value):
#         raise NotImplementedError
#
#
class RedisCache(CacheBase):
    # def get(self, key):
    #     pass
    pass


redis_cache = RedisCache()
# redis_cache.set("key", "value")
